package dao;



import entity.Emp;
import utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class EmpDao {

    public Integer update(Emp emp){
        //获取连接
        try(Connection conn = DBUtils.getConn()) {
            String sql = "update  emp set username=?,password=?,mobile=?,email=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getUsername());
            ps.setString(2,emp.getPassword());
            ps.setString(3,emp.getMobile());
            ps.setString(4,emp.getEmail());
            ps.setInt(5,emp.getId());
            Integer rows = ps.executeUpdate();
            return rows;
            //System.out.println("添加完成!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    public Integer insert(Emp emp){
        //获取连接
        try(Connection conn = DBUtils.getConn()) {
            String sql = "insert into emp values(null,?,?,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getUsername());
            ps.setString(2,emp.getPassword());
            ps.setString(3,emp.getMobile());
            ps.setString(4,emp.getEmail());
            ps.setInt(5,emp.getIsDelete());
            Integer rows = ps.executeUpdate();
            return rows;
            //System.out.println("添加完成!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    public List<Emp> findAll() {
        ArrayList<Emp> list = new ArrayList<>();
        //获取连接
        try(Connection conn = DBUtils.getConn()) {
            String sql = "select * from emp where isDelete = 0";
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery(sql);
            while(rs.next()){
                int id = rs.getInt(1);
                String username = rs.getString(2);
                String password = rs.getString(3);
                String mobile = rs.getString(4);
                String email = rs.getString(5);
                Integer isDelete = rs.getInt(6);
                //把参数封装到对象里面 并添加到list集合中
                list.add(new Emp(id, username, password,mobile,email,isDelete));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }
    public Emp findById(Integer id) {
        Emp emp = null;
        //获取连接
        try(Connection conn = DBUtils.getConn()) {
            String sql = "select * from emp where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                emp = new Emp();
                emp.setId(id);
                emp.setUsername(rs.getString("username"));
                emp.setPassword(rs.getString("password"));
                emp.setEmail(rs.getString("email"));
                emp.setMobile(rs.getString("mobile"));
                emp.setIsDelete(rs.getInt("isDelete"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return emp;
    }

    public Integer delById(Integer id) {
        Emp emp = null;
        //获取连接
        try(Connection conn = DBUtils.getConn()) {
            String sql = "update  emp set isDelete = 1 where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            Integer rs = ps.executeUpdate();
            return rs;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }
}
